package com.example.perplonews.a_d_fragment.a_news.adapter

import android.widget.ImageView
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.example.perplonews.R
import com.example.perplonews.a_b_entity.NewsEntity
import java.util.*

class News_Adapter :BaseMultiItemQuickAdapter<NewsEntity.Values,BaseViewHolder> {
    constructor(list:ArrayList<NewsEntity.Values>):super(list){
        //3.+版本时 addChildClickViewIds()
        addItemType(NewsEntity.NONECODE, R.layout.item_news4)
        addItemType(NewsEntity.IMG1CODE, R.layout.item_news1)
        addItemType(NewsEntity.IMG2CODE, R.layout.item_news3)
        addItemType(NewsEntity.IMG3CODE, R.layout.item_news2)

    }
    override fun convert(baseViewHolder: BaseViewHolder, values: NewsEntity.Values) {
        baseViewHolder.addOnClickListener(R.id.news1_title)
        baseViewHolder.addOnClickListener(R.id.news1_img)
        baseViewHolder.addOnClickListener(R.id.news1_author)
        baseViewHolder.addOnClickListener(R.id.news1_card)
        baseViewHolder.addOnClickListener(R.id.news1_time)

        baseViewHolder.addOnClickListener(R.id.news2_img1)
        baseViewHolder.addOnClickListener(R.id.news2_img2)
        baseViewHolder.addOnClickListener(R.id.news2_img3)
        baseViewHolder.addOnClickListener(R.id.news2_time)
        baseViewHolder.addOnClickListener(R.id.news3_img1)
        baseViewHolder.addOnClickListener(R.id.news3_img2)
        baseViewHolder.addOnClickListener(R.id.news3_title)
        baseViewHolder.addOnClickListener(R.id.news4_title)
        baseViewHolder.addOnClickListener(R.id.news4_author)



        //由于Android时间戳System.currentTimeMillis()获取后单位为毫秒值
        //上传服务器时值/1000,服务器获取的时间戳使用时*1000

        //由于Android时间戳System.currentTimeMillis()获取后单位为毫秒值
        //上传服务器时值/1000,服务器获取的时间戳使用时*1000
        val date: Date = Date(values.news_time * 1000)
        val timeStr = TimeUtils.getFriendlyTimeSpanByNow(date)
        if (values.getItemType() === NewsEntity.NONECODE) {
            baseViewHolder.setText(R.id.news4_title, values.news_title)
            baseViewHolder.setText(R.id.news4_author, values.news_author)
            if (values.news_time <= 0) {
                baseViewHolder.setText(R.id.news4_time, "很久以前")
                return
            }
            baseViewHolder.setText(R.id.news4_time, timeStr)
            return
        }
        if (values.getItemType() === NewsEntity.IMG1CODE) {
            baseViewHolder.setText(R.id.news1_title, values.news_title)
            baseViewHolder.setText(R.id.news1_author, values.news_author)
            if (values.news_time <= 0) {
                baseViewHolder.setText(R.id.news1_time, "很久以前")
                val img: ImageView = baseViewHolder.getView(R.id.news1_img)
                Glide.with(mContext).load("https://i01piccdn.sogoucdn.com/0248ef0ef1d0faf9").into(img)
                return
            }
            baseViewHolder.setText(R.id.news1_time, timeStr)
            val img: ImageView = baseViewHolder.getView(R.id.news1_img)
            Glide.with(mContext).load("https://i01piccdn.sogoucdn.com/082fb9a40bab4521").into(img)
            return
        }
        if (values.getItemType() === NewsEntity.IMG2CODE) {
            baseViewHolder.setText(R.id.news3_title, values.news_title)
            baseViewHolder.setText(R.id.news3_author, values.news_author)
            if (values.news_time <= 0) {
                baseViewHolder.setText(R.id.news3_time, "很久以前")
                return
            }
            baseViewHolder.setText(R.id.news3_time, timeStr)
            val img: ImageView = baseViewHolder.getView(R.id.news3_img1)
            val img1: ImageView = baseViewHolder.getView(R.id.news3_img2)
            Glide.with(mContext).load("https://i01piccdn.sogoucdn.com/082fb9a40bab4521").into(img)
            Glide.with(mContext).load("https://i01piccdn.sogoucdn.com/0248ef0ef1d0faf9").into(img1)
            return
        }
        if (values.getItemType() === NewsEntity.IMG3CODE) {
            baseViewHolder.setText(R.id.news2_title, values.news_title)
            baseViewHolder.setText(R.id.news2_author, values.news_author)
            if (values.news_time <= 0) {
                baseViewHolder.setText(R.id.news2_time, "很久以前")
                return
            }
            baseViewHolder.setText(R.id.news2_time, timeStr)
            val img: ImageView = baseViewHolder.getView(R.id.news2_img1)
            val img1: ImageView = baseViewHolder.getView(R.id.news2_img2)
            val img2: ImageView = baseViewHolder.getView(R.id.news2_img3)
            Glide.with(mContext).load("https://i02piccdn.sogoucdn.com/0de8dcfc26ef408f").into(img)
            Glide.with(mContext).load("https://i02piccdn.sogoucdn.com/0de8dcfc26ef408f").into(img1)
            Glide.with(mContext).load("https://i02piccdn.sogoucdn.com/367da58fe13055a5").into(img2)
            return
        }

    }




}